﻿using SHZY.Common.Core.Manager;
using SHZY.Common.Core.Security;
using SHZY.Common.Enum;
using SHZY.Common.Extension;
using SHZY.Common.Filter;
using SHZY.Common.Models;
using SHZY.Common.Security;
using SHZY.DependencyInjection;
using SHZY.DynamicApiController;
using SHZY.FriendlyException;
using SHZY.Systems.Entitys.Permission;
using SHZY.Systems.Entitys.System;
using SHZY.Systems.Interfaces.System;
using SHZY.Business.Entitys.Dto.QmsBusiDefecthandleDetail;
using SHZY.Business.Entitys;
using SHZY.Business.Interfaces;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace SHZY.Business;

/// <summary>
/// 业务实现：不合格品处理单不良项.
/// </summary>
[ApiDescriptionSettings(Tag = "Business", Name = "DefecthandleDetail", Order = 200)]
[Route("api/Business/[controller]")]
public class QmsBusiDefecthandleDetailService : IQmsBusiDefecthandleDetailService, IDynamicApiController, ITransient
{
    /// <summary>
    /// 服务基础仓储.
    /// </summary>
    private readonly ISqlSugarRepository<QmsBusiDefecthandledetailEntity> _repository;

    /// <summary>
    /// 数据接口服务.
    /// </summary>
    private readonly IDataInterfaceService _dataInterfaceService;

    /// <summary>
    /// 用户管理.
    /// </summary>
    private readonly IUserManager _userManager;

    /// <summary>
    /// 初始化一个<see cref="QmsBusiDefecthandleDetailService"/>类型的新实例.
    /// </summary>
    public QmsBusiDefecthandleDetailService(
        ISqlSugarRepository<QmsBusiDefecthandledetailEntity> qmsBusiDefecthandledetailRepository,
        IDataInterfaceService dataInterfaceService,
        ISqlSugarClient context,
        IUserManager userManager)
    {
        _repository = qmsBusiDefecthandledetailRepository;
        _dataInterfaceService = dataInterfaceService;
        _userManager = userManager;
    }

    /// <summary>
    /// 获取不合格品处理单不良项.
    /// </summary>
    /// <param name="id">主键值.</param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<dynamic> GetInfo(string id)
    {
        return (await _repository.FirstOrDefaultAsync(x => x.Id == id)).Adapt<QmsBusiDefecthandledetailInfoOutput>();
    }

    /// <summary>
    /// 获取不合格品处理单不良项列表.
    /// </summary>
    /// <param name="input">请求参数.</param>
    /// <returns></returns>
    [HttpGet("")]
    public async Task<dynamic> GetList([FromQuery] QmsBusiDefecthandledetailListQueryInput input)
    {
        var data = await _repository.Context.Queryable<QmsBusiDefecthandledetailEntity>()
            .OrderByIF(string.IsNullOrEmpty(input.sidx), it => it.Id)
            .Select(it => new QmsBusiDefecthandledetailListOutput
            {
                id = it.Id,
                innspectionName = it.InnspectionName,
                samplingQty = it.SamplingQty,
                defectQty = it.DefectQty,
                defectRate = it.DefectRate,
            }).OrderByIF(!string.IsNullOrEmpty(input.sidx), it => input.sidx + " " + input.sort).ToPagedListAsync(input.currentPage, input.pageSize);
        return PageResult<QmsBusiDefecthandledetailListOutput>.SqlSugarPageResult(data);
    }

}